package com.jsmtr.rms.controller.DUserResume;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.EasyShuttleService.impl.EasyShuttleService;
import com.jsmtr.rms.service.*;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.ResultBean;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class DUserResumeEasyShuttleController {

    @Autowired
    private EasyShuttleService easyShuttleService;
    @Autowired
    private IBaseExaminerService baseExaminerService;
    @Autowired
    private IBaseEvaluationService baseEvaluationService;
    @Autowired
    private IDoMSService msService;

    @Resource
    private IDoGXService doGXService;

    public EasyShuttleService getEasyShuttleService() {
        return easyShuttleService;
    }

    public void setEasyShuttleService(EasyShuttleService easyShuttleService) {
        this.easyShuttleService = easyShuttleService;
    }

    @Resource
    private IUtilService utilService;



    @RequestMapping(value = "/Easy/EasyForm",produces = "application/json;charset=utf-8")
    public String EasyForm(HttpServletRequest request, HttpServletResponse response, Model model){
        try{
            String pid= request.getParameter("pid");
            String fid = request.getParameter("fid");
            String depcode = request.getParameter("deptcode");
            model.addAttribute("pid",pid);
            model.addAttribute("fid",fid);
            model.addAttribute("deptcode",depcode);
            List<ProjectInfoJobs> projectJobs=utilService.getProjectJobs(pid);
            model.addAttribute("jobs",projectJobs);
            ProjectInfoFlow flow=utilService.getFlow(fid);

            model.addAttribute("flow",flow.getFname());
            if ("资料审核".equalsIgnoreCase(flow.getFname())){
                return "/Do/Easy/zleasy";
            }else if("面试安排".equalsIgnoreCase(flow.getFname())||"面试".equalsIgnoreCase(flow.getFname())){
                return "/Do/Easy/mseasy";
            }else if("体检".equalsIgnoreCase(flow.getFname())){
                return "/Do/Easy/tjeasy";
            }else{
                return "/Do/Easy/index";
            }
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @ResponseBody
    @RequestMapping(value = "/Easy/EasyConfrimMsg",produces = "application/json;charset=utf-8")
    public ResultBean EasyConfrimMsg(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();

        return resultBean;
    }

    @ResponseBody
    @RequestMapping(value = "/Easy/EasyPublic",produces = "application/json;charset=utf-8")
    public ResultBean EasyPublic(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();

        return resultBean;
    }

    @ResponseBody
    @RequestMapping(value = "/Easy/EasyShut",produces = "application/json;charset=utf-8")
    public ResultBean EasyShut(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();

        return resultBean;
    }

    @ResponseBody
    @RequestMapping(value = "/Easy/AssignmentAction",produces = "application/json;charset=utf-8")
    public ResultBean EasyAssignmentAction(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();
        String pid= request.getParameter("pid");
        String fid = request.getParameter("fid");
        String jname=request.getParameter("jname");
        String depcode = request.getParameter("deptcode");
        String allowcount=request.getParameter("allowcount");
        BaseUser cuser = utilService.getSessionUser(request);
        String checkColumn="";
        try{
            ProjectInfoFlow flow=utilService.getFlow(fid);
            if ("资料审核".equalsIgnoreCase(flow.getFname())){
                String kstime=request.getParameter("kstime");
                String jstime=request.getParameter("jstime");
                String zladdress=request.getParameter("zladdress");

                String zlzysx=request.getParameter("zlzysx");
                checkColumn="zladdress";
                if (StringTools.trimNotEmpty(allowcount)){
                    int count=Integer.parseInt(allowcount);
                    Map params=new HashMap();
                    params.put("zlkstime",kstime);
                    params.put("zljstime",jstime);
                    params.put("zladdress",zladdress);
                    params.put("zlremark",zlzysx);
                    if(StringTools.trimNotEmpty(zlzysx)&& StringTools.trimNotEmpty(allowcount)&& StringTools.trimNotEmpty(zladdress)) {
                        Integer res = easyShuttleService.EasyAssignment(pid, fid, jname, params, count, checkColumn);
                        easyShuttleService.EasyZysx(pid,fid,jname, Integer.parseInt(allowcount),checkColumn,cuser,zlzysx);
                        if (res > 0) {
                            resultBean.setState(true);
                            resultBean.setMessage("操作成功!");
                        }
                    }
                }
            }else if("面试".equalsIgnoreCase(flow.getFname())||"面试安排".equalsIgnoreCase(flow.getFname())){
                String starttime = request.getParameter("starttime").trim();//面试时间
                String address = request.getParameter("address").trim();//面试地点
                String endtime =request.getParameter("endtime");//确认截止时间
                String mszysx=request.getParameter("mszysx");
               boolean re= createMsPlan(pid,fid,depcode,starttime,endtime,address,mszysx,jname,cuser,allowcount);
                if (re) {
                    resultBean.setState(true);
                    resultBean.setMessage("操作成功!");
                }
            }else if("体检".equalsIgnoreCase(flow.getFname())){
                String tjtime=request.getParameter("tjtime");
                String tjaddress=request.getParameter("tjaddress");
                String tjzysx=request.getParameter("tjzysx");
                checkColumn="tjaddress";
                Map params=new HashMap();
                params.put("tjtime",tjtime);
                params.put("tjaddress",tjaddress);
                int count=Integer.parseInt(allowcount);
                if( StringTools.trimNotEmpty(allowcount)&& StringTools.trimNotEmpty(tjaddress)){
                    Integer res = easyShuttleService.EasyAssignment(pid, fid, jname, params, count, checkColumn);
                    easyShuttleService.EasyZysx(pid,fid,jname, Integer.parseInt(allowcount),checkColumn,cuser,tjzysx);
                    if (res > 0) {
                        resultBean.setState(true);
                        resultBean.setMessage("操作成功!");
                    }

                }
            }
        }catch(Exception e){
            e.printStackTrace();
              resultBean.setState(false);
              resultBean.setMessage("操作失败!");
        }
        return resultBean;
    }


    private boolean createMsPlan(String pid, String fid, String deptcode, String starttime, String endtime, String address, String mszysx, String jname, BaseUser cuser, String count){
        try{

            String examinerid = "55dae756-8a50-4f3b-8a76-25a7787c8236";//面试组ID
            String examinername = baseExaminerService.getGroup(examinerid).getName();//面试组名称
            String evaluationid = "6f9749d5-40ff-46c3-a297-26fe4a9f2210";//评价表ID
            String evaluationname = baseEvaluationService.getTable(evaluationid).getName();//评价表名称

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("notice", "-1");
            _map.put("flowid", fid);
            _map.put("msplan", "NO");
            _map.put("isconfirm", "");

            DUserResume dUserResume=new DUserResume();
            dUserResume.setJname(jname);
            dUserResume.setPath(fid);
            dUserResume.setProid(pid);
            Map<String, Object> map = doGXService.getMSUsers(1, Integer.parseInt(count), dUserResume, _map);


            if (cuser.getUdept().equals(deptcode)) {
                if (utilService.FlowIsDoing(fid)) {
                    List list = new ArrayList();
                    String gid = UUID.randomUUID().toString();
                    /*//面试组
                    List<BaseExaminer> examiners = baseExaminerService.getBaseExaminers(examinerid);
                    for (BaseExaminer examiner : examiners) {
                        DoMSExaminer dest = new DoMSExaminer();
                        BeanTools.Copy(examiner, dest);
                        dest.setId(UUID.randomUUID().toString());
                        dest.setGid(gid);
                        dest.setFid(fid);
                        list.add(dest);
                    }
                    //评价表
                    List<BaseEvaluationItem> items = baseEvaluationService.getBaseEvaluationItems(evaluationid);
                    for (BaseEvaluationItem item : items) {
                        DoMSEvaluation dest = new DoMSEvaluation();
                        BeanTools.Copy(item, dest);
                        dest.setId(UUID.randomUUID().toString());
                        dest.setGid(gid);
                        list.add(dest);
                    }*/

                    int ccount = 1 + msService.getMsCount(fid);

                    List editList = new ArrayList();
                    List<HashMap> objectList = (List<HashMap>)map.get("list");
                    //面试计划
                    for (HashMap<String,Object> _user : objectList) {
                        String uid=_user.get("ID").toString();
                        DoMSUser cmsuser = msService.getMsUser(fid, uid);
                        if (cmsuser == null) {
                            DoMSUser msuser = new DoMSUser();
                            msuser.setId(UUID.randomUUID().toString());
                            msuser.setAddress(address);
                            msuser.setEvaluationname(evaluationname);
                            msuser.setExaminercount(3);
                            msuser.setExaminername(examinername);
                            msuser.setFid(fid);
                            msuser.setGid(gid);
                            msuser.setNotice(EnumTools.NoticeState.未发通知.ordinal());
                            msuser.setResult("未面试");
                            msuser.setStarttime(starttime);
                            msuser.setUserid(uid);
                            msuser.setPointuser(",");
                            msuser.setNum(String.valueOf(count));
                            list.add(msuser);
                            if(endtime!=null&&endtime.trim().length()>0){
                                DoMSConfirm msConfirm=new DoMSConfirm();
                                msConfirm.setId(UUID.randomUUID().toString());
                                msConfirm.setMsuserid(msuser.getId());
                                msConfirm.setEndtime(endtime.trim());
                                msConfirm.setIsconfirm("否");
                                msConfirm.setType("MS");
                                list.add(msConfirm);
                            }
                            if(mszysx!=null&&mszysx.trim().length()>0){
                                DoZysx zysx=new DoZysx();
                                zysx.setId(UUID.randomUUID().toString());
                                zysx.setFid(fid);
                                zysx.setPid(pid);
                                zysx.setRemark(mszysx);
                                zysx.setUserid(uid);
                                zysx.setType("MS");
                                zysx.setAddtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                zysx.setAdduser(cuser.getUcode());
                                list.add(zysx);
                            }
                            ccount++;

                        }else{
                            cmsuser.setAddress(address);
                            cmsuser.setEvaluationname(evaluationname);
                            cmsuser.setExaminercount(3);
                            cmsuser.setExaminername(examinername);
                            cmsuser.setFid(fid);
                            cmsuser.setGid(gid);
                            if(!cmsuser.getNotice().equals(EnumTools.NoticeState.已发通知.ordinal())){
                                cmsuser.setNotice(EnumTools.NoticeState.未发通知.ordinal());
                            }
                            if(cmsuser.getResult().equalsIgnoreCase("未面试")){
                                cmsuser.setUserid(uid);
                                cmsuser.setPointuser("");
                                cmsuser.setNum(String.valueOf(count));
                            }
                            cmsuser.setStarttime(starttime);
                            editList.add(cmsuser);
                            if(endtime!=null&&endtime.trim().length()>0){
                                DoMSConfirm msConfirm=utilService.getMSUserConfirm(cmsuser.getId());
                                if(msConfirm==null){
                                    msConfirm=new DoMSConfirm();
                                    msConfirm.setId(UUID.randomUUID().toString());
                                    msConfirm.setMsuserid(cmsuser.getId());
                                    msConfirm.setEndtime(endtime.trim());
                                    msConfirm.setIsconfirm("否");
                                    msConfirm.setType("MS");
                                    list.add(msConfirm);
                                }else{
                                    if(msConfirm.getIsconfirm().equalsIgnoreCase("否")){
                                        msConfirm.setEndtime(endtime.trim());
                                        editList.add(msConfirm);
                                    }
                                }

                            }else{
                                DoMSConfirm msConfirm=utilService.getMSUserConfirm(cmsuser.getId());
                                if(msConfirm!=null){
                                    utilService.delObject(msConfirm);
                                }
                            }
                            if(mszysx!=null&&mszysx.trim().length()>0){
                                DoZysx zysx=utilService.getUserZysx(pid,fid,uid);
                                if(zysx==null){
                                    zysx=new DoZysx();
                                    zysx.setId(UUID.randomUUID().toString());
                                    zysx.setFid(fid);
                                    zysx.setPid(pid);
                                    zysx.setRemark(mszysx);
                                    zysx.setUserid(uid);
                                    zysx.setType("MS");
                                    zysx.setAddtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                    zysx.setAdduser(cuser.getUcode());
                                    list.add(zysx);
                                }else {
                                    zysx.setRemark(mszysx);
                                    zysx.setType("MS");
                                    zysx.setAddtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                    zysx.setAdduser(cuser.getUcode());
                                    editList.add(zysx);
                                }
                            }else{
                                DoZysx zysx=utilService.getUserZysx(pid,fid,uid);
                                if(zysx!=null){
                                    utilService.delObject(zysx);
                                }
                            }
                        }
                    }

                    utilService.editList(editList, list);
                    return  true;
                } else {
                    return  false;

                }
            } else {
                return  false;
            }
        }catch(Exception e){
            e.printStackTrace();
            return  false;
        }

    }
}
